package com.soriya.nestlive.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soriya.nestlive.entity.CategoryEntity;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.param.CategoryListParam;
import com.soriya.nestlive.service.CategoryService;
import com.soriya.nestlive.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class CategoryApi {

    private final CategoryService categoryService;

    @Autowired
    public CategoryApi(CategoryService categoryService) {
        this.categoryService = categoryService;
    }


    @GetMapping
    public Result<?> getAll() {
        List<CategoryEntity> list = categoryService.list();
        return Result.success(list);
    }

    @GetMapping("/{current}/{size}")
    public Result<?> list(@PathVariable Integer current, @PathVariable Integer size, CategoryListParam param) {
        Page<CategoryEntity> page = new Page<>(current, size);
        PageResult<CategoryEntity> result = categoryService.pageQuery(page, param);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<CategoryEntity> getById(@PathVariable Long id) {
        CategoryEntity data = categoryService.getById(id);
        return Result.success(data);
    }


}
